<template>
  <div>
    <!-- 选择自动生成配置项 2018/3/7 11:44 zhangQ -->
    <Table border :columns="generationColumns" :data="generationConfigs"></Table>
  </div>
</template>

<script>
  import initDb from '@/components/common/indexeddbUtil'
  import _ from 'lodash'

  export default {
    name: 'generation-config-list',
    data () {
      let generationConfigs = []
      let generationColumns = [
        {
          title: '数据名称',
          key: 'configName'
        },
        {
          title: '操作',
          width: 210,
          render: (h, params) => {
            let checkBtn = (<i-button type="primary" onClick={() => this.checkConfig(params.row)}>选择</i-button>)
            let deleteBtn = (<i-button type="warning" style="margin-left:15px" onClick={() => this.deleteConfig(params.row)}>删除</i-button>)
            return ([checkBtn, deleteBtn])
          }
        }
      ]
      let datas = { generationColumns, generationConfigs }
      return datas
    },
    computed: {},
    methods: {
      /**
       * TODO(选择配置)
       * @param config:::选择的配置
       * @auther zhangQ
       * @date 2018/5/3 11:25
       */
      checkConfig (config) {
        this.$emit('generationConfig', config)
        this.$emit('close')
      },
      /**
       * TODO(删除配置)
       * @auther zhangQ
       * @date 2018/5/3 12:24
       */
      deleteConfig (config) {
        let vue = this
        initDb(contant.storeNames.generationStore.name).then((store) => {
          let request = store.delete(config[contant.storeNames.generationStore.index])
          request.onsuccess = function () {
            let index = _.findIndex(vue.generationConfigs, (o) => {
              return config[contant.storeNames.generationStore.index] === o[contant.storeNames.generationStore.index]
            })
            vue.generationConfigs.splice(index, 1)
            console.log('成功')
          }
          request.onerror = function (e) {
            console.log('失败')
            vue.$Notice.error({
              title: '保存失败',
              desc: '保存名称不能重复'
            })
          }
        })
      }
    },
    created () {
    },
    mounted () {
      let that = this
      initDb(contant.storeNames.generationStore.name).then((store) => {
        let cursor = store.openCursor()
        cursor.onsuccess = function (e) {
          let res = e.target.result
          if (res && res.value) {
            if (res.value.configName) {
              that.generationConfigs.push(res.value)
            }
            res.continue()
            // 5-continue方法将光标移到下一个数据对象，如果当前数据对象已经是最后一个数据了，则光标指向null。
          }
        }
      })
    },
    components: {}
  }
</script>

<style scoped>

</style>